﻿using MyBBSWebApi.DAL;
using MyBBSWebApi.Models;
using System;
using System.Collections.Generic;

namespace MyBBSWebApi.BLL
{
    public class UserBll : IUserBll
    {
        UserDal userDal = new UserDal();

        public List<Users> GetAll()
        {
            return userDal.GetAll().FindAll(m => !m.IsDelete);
        }
        public Users CheckLogin(string userNo, string password)
        {
            List<Users> userList = userDal.GetUserByUserNoAndPwd(userNo, password);
            if (userList.Count <= 0)
            {
                return default;
            }
            else
            {
                Users user = userList.Find(m => !m.IsDelete);
                return user;
            }
        }

        public string AddUser(string userNo, string userName, int userLevel, string password)
        {
            UserDal userDal = new UserDal();
            int rows = userDal.AddUser(userNo, userName, userLevel, password);
            if (rows > 0)
            {
                return "数据新增成功";
            }
            else
            {
                return "数据新增失败";
            }
        }

        public string UpdateUser(int id, string userNo, string userName, string password, int? userLevel)
        {
            UserDal userDal = new UserDal();
            int rows = userDal.UpdateUser(id, userNo, userName, userLevel, password);
            if (rows > 0)
            {
                return "数据修改成功";
            }
            else
            {
                return "数据修改失败";
            }
        }

        public string RemoveUser(int id)
        {
            UserDal userDal = new UserDal();
            int rows = userDal.RemoveUser(id);
            if (rows > 0)
            {
                return "删除修改成功";
            }
            else
            {
                return "删除修改失败";
            }
        }


        private Users GetLoginResult(List<Users> userList, bool isPasswordLogin)
        {
            Users user = userList.Find(m => !m.IsDelete);
            user.Token = Guid.NewGuid();
            if (isPasswordLogin)
            {
                user.AutoLoginTag = Guid.NewGuid();
            }
            UpdateUser(user.Id, user.UserNo, user.UserName, user.Password, user.UserLevel);
            return user;

        }


    }
}
